//
//  ViewController.m
//  TestTableViewCellClick
//
//  Created by apple on 17/4/7.
//  Copyright © 2017年 wax. All rights reserved.
//

#import "ViewController.h"

#import "TestTableViewCell.h"

#import "DemoTwoViewController.h"

#import "Btn1ViewController.h"

#import "Btn2ViewController.h"



@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,ApprovalIconClickProtocol>

@property (nonatomic,weak)UITableView *tableView;

@property (nonatomic,copy)NSArray *MYdataArray;

@property (nonatomic,assign)NSInteger YHPreTag;


@end

static NSString *cellID =@"TestTableViewCell";


@implementation ViewController{

    //cell是否触发行点击
    BOOL _YHIsPush;
    
}

#pragma mark - 生命周期

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //prapareTableView
    [self prapareTableView];
    
    //模拟数据请求
    [self getdatas];
    
    //初始化设置
    _YHPreTag = -1;
    
    //接收通知
    [self prapareCellNotis];

}

#pragma mark - 创建控件
-(void)prapareTableView{
    
    UITableView *tableView = [[UITableView alloc]init];
    
    tableView.frame = self.view.bounds;
    
    [self.view addSubview:tableView];
    
    tableView.dataSource = self;
    
    tableView.delegate = self;
    
    _tableView = tableView;
}

#pragma mark - 接收通知
-(void)prapareCellNotis{

    //仅通过上一次的按钮状态 - 控制界面跳转
    [self prapareCellClickNoti];
    
    //回传点击当行的tag
    [self prepareCellCurrentTag];
    
}

//回传当前点击的cell的下标
-(void)prepareCellCurrentTag{
    //数据回传
    NSString * cellClickNoti = @"cellClickIndexTagNoti";
    
    [[NSNotificationCenter defaultCenter]addObserverForName:cellClickNoti object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        
        NSInteger currentTag = [note.object intValue];
        
        //当前点击的Btn
        NSLog(@"当前点击的Btn --> %ld,上一个点击的是 --> %ld",(long)currentTag,_YHPreTag);
        
        //如果还未点击过-> 初始值默认是-1
        if (_YHPreTag == -1 || _YHPreTag == currentTag) {
            
            //说明点击的是第一个
            NSLog(@"说明点击的是第一个");
        }
        
        else{
        
            //取消上一个
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_YHPreTag inSection:0];
            
            TestTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

            cell.showView.hidden= YES;
            
            NSLog(@"取消上一个选择状态"); //-> 通过下标找cell？
        }
        
        _YHPreTag = currentTag;
        
    }];
    
    
}

//设置是否可以触发cell行点击

-(void)prapareCellClickNoti{

    //默认
    _YHIsPush = YES;
    
    //发送通知，控制tableViewCell的点击事件
    NSString * testCellClickNoti = @"testCellClickNoti";
    
    
    [[NSNotificationCenter defaultCenter]addObserverForName:testCellClickNoti object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        
        NSLog(@"note=%@",note.object);
        
        switch ([note.object intValue]) {
            case 0:
            {
                //cell不可点击(不可跳转！)
                _YHIsPush = NO;
                break;
            }
            case 1:
            {
                //cell可以点击
                _YHIsPush = YES;
                
                break;
            }
            default:
                break;
        }
        
    }];
}


#pragma mark -  模拟网络请求
-(void)getdatas{

    //AFN

    NSMutableArray *arrayM = [NSMutableArray array];
    
    NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
    //请求得到的数据
    for (int i = 0; i<20;i++) {
        
        [dictM setValue:@(i) forKey:@"index"];
        
        //随机数
        NSInteger myID = arc4random_uniform(100000);
        
        [dictM setValue:@(myID) forKey:@"userID"];

        [arrayM addObject:dictM.copy];
    }
    _MYdataArray = arrayM.copy;
    
}

-(void)ApprovalIconPush:(NSInteger)indexTag{

    if (indexTag == 1) {
        
        Btn1ViewController *btn1VC = [[Btn1ViewController alloc]init];

        [self.navigationController pushViewController:btn1VC animated:YES];
    }
    else if(indexTag == 2){
        Btn2ViewController *btn2VC = [[Btn2ViewController alloc]init];
        
        [self.navigationController pushViewController:btn2VC animated:YES];
        
    }
}

#pragma mark - <tableViewDelegate>

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _MYdataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (cell == nil) {
        
        cell = [TestTableViewCell loadCell];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.delegate = self;
    
    cell.indexTag = indexPath.row;
    

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 120;
    
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (!_YHIsPush) {
       
        NSLog(@"如果backView出现 --> Cell不可跳转！！");
    }
    
    else{
        DemoTwoViewController *twoVC = [[DemoTwoViewController alloc]init];
        
        [self.navigationController pushViewController:twoVC animated:YES];
    }
    
}




@end
